package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.order.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * lcd  2020/6/4
 * Description:
 */
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> queryListByPid(Long pid) {
        Category t = new Category();
        t.setParentId(pid);
        List<Category> list = categoryMapper.select(t);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return list;
    }

    public String save(Category category) {
        int insert = categoryMapper.insert(category);
        if (insert == 1) {
            return "success add";
        }
        return "fail add";
    }

    public String update(Category category) {
        int result = categoryMapper.updateByPrimaryKey(category);
        if (result == 1) {
            return "success update";
        }
        return "fail update";
    }

    public String delete(Long id) {
        int result = categoryMapper.deleteByPrimaryKey(id);
        if (result == 1) {
            return "success delete";
        }
        return "fail delete";
    }

    public List<Category> queryByIds(List<Long> ids) {
        List<Category> list = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return list;
    }
}
